package admin.subject;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.CommonUtil;
import com.common.Dateutil;
import com.common.ResultData;
import com.filter.BaseController;
import com.intercepter.AuthPassport;

import beans.subject.ChapterModelSearch;
import beans.subject.SpecialBean;
import beans.subject.SubinfoModel;
import beans.subject.subchapterBean;
import beans.subject.subinfo;
import beans.sys.SYSCodeBean;
import beans.sys.UserBean;
import service.subject.SubChapterServiceImpl;
import service.subject.SubInfoServiceImpl;
import service.subject.SubSpecialServiceImpl;
import service.sys.SYSCodeServiceImpl;
@Controller
@RequestMapping("/subinfo")
public class SubInfoController extends BaseController{
	
	@Resource
	private SubInfoServiceImpl<subinfo> subInfoServiceImpl;

	@Resource
	private SYSCodeServiceImpl<SYSCodeBean> _SYSCodeServiceImpl;

	@Resource
	private SubChapterServiceImpl<subchapterBean> _SubChapterServiceImpl;
	
	@Resource
	private SubSpecialServiceImpl<SpecialBean> _SubSpecialServiceImpl;
	
	@AuthPassport(actionName="index",controllerName="subject")
	@RequestMapping("/index")
	public String Index() {
		List<Map> list = _SYSCodeServiceImpl.getListByCode("CarType");
		getReq().setAttribute("CarTypeList", list);
		
		return "admin/subjectinfo/index";
	}
	
	@RequestMapping("/GetData")
	@ResponseBody
	@AuthPassport(actionName="index",controllerName="subject")
	public void DataGrid(SubinfoModel model){
		ResultData<List<subinfo>> rd = new ResultData<>();
		try{
			UserBean u = (UserBean)getReq().getSession().getAttribute("user");

			rd.setPage(model.getPage());

			List<subinfo> m = subInfoServiceImpl.queryByList(model);
			
			rd.setRows(m);
			rd.setRecords(model.getPager().getRowCount());
			rd.setTotal(model.getPager().getPageCount());
		}
		catch(Exception e){
			e.printStackTrace();
		}
		responseJson(rd);
	}
	
	/***
	 * 根据题目类型和科目类型获取章节和题型
	 * @param drivertype
	 * @param subtype
	 */
	@RequestMapping("/GetChapterSpecial")
	@ResponseBody
	private void GetChapterSpecial(String drivertype,String subtype) {
		List<subchapterBean> chapterList = _SubChapterServiceImpl.getByDriverAndSub(drivertype,subtype);
		List<SpecialBean> specialList = _SubSpecialServiceImpl.getByDriverAndSub(drivertype,subtype);
		HashMap<String, Object> m = new HashMap<>();
		m.put("chapter", chapterList);
		m.put("special", specialList);

		ResultData<HashMap<String, Object>> rd = new ResultData<>();
		rd.setType(1);
		rd.setRows(m);
		responseJson(rd);
	}

	@AuthPassport(actionName="index",controllerName="subject")
	@RequestMapping("/detail")
	public String Detail(String id) {
		subinfo m = null;
		if(!CommonUtil.isNullObject(id)){
			try {
				m = subInfoServiceImpl.queryById(id);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		List<Map> list = _SYSCodeServiceImpl.getListByCode("CarType");

		getReq().setAttribute("CarTypeList", list);
		getReq().setAttribute("chapter", m);
		return "admin/subjectinfo/detail";
	}

	@AuthPassport(actionName="index",controllerName="subject")
	@RequestMapping("/save")
	@ResponseBody
	public void Save(subinfo m) {
		ResultData<String> rd = new ResultData<>();
		try {
			UserBean u = (UserBean)getReq().getSession().getAttribute("user");
			if(CommonUtil.isNullObject(m.getId())) {
				m.setId(UUID.randomUUID().toString());
				subInfoServiceImpl.add(m);
				
				subInfoServiceImpl.updateChapterSpecial(m.getChapterid(),m.getSpecialid());
			}
			else {
				subInfoServiceImpl.update(m);
			}
		} catch (Exception e) {
			rd.setMessage("系统错误,请联系管理员!");
			e.printStackTrace();;
			responseJson(rd);
			return;
		}
		rd.setType(1);
		rd.setMessage("保存成功!");
		responseJson(rd);
	}

	@AuthPassport(actionName="index",controllerName="subject")
	@RequestMapping("/delete")
	@ResponseBody
	public void Delete() {
		String[] ids = getReq().getParameterValues("ids");
		try{
			List<subinfo> list = subInfoServiceImpl.getByIds(ids);
			HashMap<String,Integer> hmc = new HashMap<>();
			HashMap<String,Integer> hms = new HashMap<>();
			String sqlc = "UPDATE sub_chapter SET subcount=subcount-%s WHERE id='%s';";
			String sqlm = "UPDATE sub_special SET subcount=subcount-%s WHERE id='%s';";
			String temSql = "";
			for(subinfo s : list) {
				if(hmc.containsKey(s.getChapterid()))hmc.put(s.getChapterid(), hmc.get(s.getChapterid()+1));
				else hmc.put(s.getChapterid(), 1);
				
				if(hms.containsKey(s.getSpecialid()))hms.put(s.getSpecialid(), hms.get(s.getSpecialid()+1));
				else hms.put(s.getSpecialid(), 1);
			}
			for (String key : hmc.keySet()) {
				temSql += String.format(sqlc, hmc.get(key),key);
			}
			for (String key : hms.keySet()) {
				temSql += String.format(sqlm, hms.get(key),key);
			}
			subInfoServiceImpl.deletes(ids,temSql);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		ResultData<String> rd = new ResultData<>();
		rd.setType(1);
		rd.setMessage("删除成功");
		responseJson(rd);
	}
}
